package com.Prot2.Logger;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import com.Prot2.Globals;

public class CoordinatorFile extends Service {

	private static final String TAG = "Coordinator";
	protected static final String DELIMITER = ",";
	private Context context;
	private		Timer t;
	
	public void onCreate(){
		context=this;
		super.onCreate();
		
		//TODO monitor changes to this string...
		String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		Log.w(TAG,"GPS: "+providers);
		
		Log.w(TAG,"Service created, fp: "+Globals.logFilePath);
		
		startThreads();
		//*///////////////////////////////////////////FILE LOGGING////////////////////////////////////////////////////////////////////
		Globals.logFilePath=Globals.dirPath+"log_"+Globals.getDateAndTimeStamp()+".txt";	
		Globals.createLogFile();
		Globals.makeFileEntry(Globals.logHeader(DELIMITER), Globals.logFile);
		t = new Timer();
		t.scheduleAtFixedRate(new LogEntry(), 0, Globals.samplingRate);
		//*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		/*///////////////////////////////////////////ARRAY LOGGING/////////////////////////////////////////////////////////////////////
		Globals.dataTable = new float [Globals.samplingTime/Globals.samplingRate][15];
		t = new Timer();
		t.scheduleAtFixedRate(new Routine(), 0, Globals.samplingTime+Globals.waitTime); //wait time can be adjusted to run recognitions less/more frequently. It also serves as a buffer to make up for interrupts from the OS-level.
		*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		Toast.makeText(this, "started logging" ,Toast.LENGTH_SHORT).show();
		
	
	}
	
	private void startThreads(){
		Globals.accelerationWorker = new AccelerationWorker(context);
		//Globals.gyroWorker = new GyroWorker(context);
		Globals.lightWorker = new LightWorker(context);
		Globals.humidityWorker = new HumidityWorker(context);
		Globals.temperatureWorker = new TemperatureWorker(context);
		Globals.pressureWorker = new PressureWorker(context);
		Globals.gpsWorker = new GPSWorker(context);
		Globals.wifiWorker = new WiFiWorker(context);
		Globals.batteryWorker = new BatteryWorker(context);
	}
	
	private void stopThreads(){
		Globals.accelerationWorker.stop();
		//Globals.gyroWorker.stop();
		Globals.lightWorker.stop();
		Globals.humidityWorker.stop();
		Globals.temperatureWorker.stop();
		Globals.pressureWorker.stop();
		Globals.gpsWorker.stop();
		Globals.wifiWorker.stop();
		Globals.batteryWorker.stop(context);
		t.cancel();
	}
	
	public void onDestroy(){
		stopThreads();
		Globals.dirPath=Environment.getExternalStorageDirectory()+"/"+Globals.appPath+"/DATA/"+Globals.getLogFolder()+"/";
	//	Globals.logFilePath=Globals.dirPath+"logfile.txt";
		Toast.makeText(this, "stopped logging" ,Toast.LENGTH_LONG).show();
	}
	
	
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	

	private class LogEntry extends TimerTask{
		
		private String logEntry(String DELIMITER){
			return ""+System.currentTimeMillis()
					// +DELIMITER+Globals.now.hour+":"+Globals.now.minute+":"+Globals.now.second
					 +DELIMITER+Globals.getDateAndTimeStamp()
					 +DELIMITER+Globals.accelerationWorker.getRead(DELIMITER)
					// +DELIMITER+Globals.gyroWorker.getRead(DELIMITER)
					 +DELIMITER+Globals.lightWorker.getRead(DELIMITER)
					 +DELIMITER+Globals.humidityWorker.getRead(DELIMITER)
					 +DELIMITER+Globals.temperatureWorker.getRead(DELIMITER)
					 +DELIMITER+Globals.pressureWorker.getRead(DELIMITER)
					 +DELIMITER+Globals.currentLocationTag
					 +DELIMITER+Globals.gpsWorker.getRead(DELIMITER)
					 +DELIMITER+Globals.wifiWorker.getRead(DELIMITER)
					 +DELIMITER+Globals.batteryWorker.getRead(DELIMITER)
					 +System.getProperty("line.separator");
		}
		
		@Override
		public void run() {
			//make a log entry..
		
			String str = logEntry(DELIMITER);
			Log.w(TAG,"making log entry: "+str);
		
			Globals.makeFileEntry(str, Globals.logFile);
		

		}
		
	}
	
	private class Routine extends TimerTask{
		
		public void run(){
			Timer t1=new Timer();
			
			Log.w(TAG,Globals.dataTable[10][0]+"");
			Log.w(TAG,Globals.dataTable[10][1]+"");
			Log.w(TAG,Globals.dataTable[10][2]+"");
			Log.w(TAG,Globals.dataTable[10][3]+"");
			Log.w(TAG,Globals.dataTable[10][4]+"");
			Log.w(TAG,Globals.dataTable[10][5]+"");
			Log.w(TAG,Globals.dataTable[10][6]+"");
			Log.w(TAG,Globals.dataTable[10][7]+"");
			
			Globals.dataTableCounter=0;
			Log.w(TAG,"Generating new buffertable");
			Globals.dataTable = new double [Globals.samplingTime/Globals.samplingRate][15];
			t1.scheduleAtFixedRate(new BufferUpdate(), 0, Globals.samplingRate);
			//run recognition routine

			
		}
		
		private class BufferUpdate	extends TimerTask{
		
			private static final String TAG = "RecognitionWorker";

			public void run(){
				if(Globals.dataTableCounter<Globals.samplingTime/Globals.samplingRate){
					Globals.dataTable[Globals.dataTableCounter][0]=System.currentTimeMillis();
					Globals.dataTable[Globals.dataTableCounter][1]=Globals.accelerationWorker.getXAcc();
					Globals.dataTable[Globals.dataTableCounter][2]=Globals.accelerationWorker.getYAcc();
					Globals.dataTable[Globals.dataTableCounter][3]=Globals.accelerationWorker.getZAcc();
					Globals.dataTable[Globals.dataTableCounter][4]=Globals.lightWorker.getLux();
					Globals.dataTable[Globals.dataTableCounter][5]=Globals.humidityWorker.getRH();
					Globals.dataTable[Globals.dataTableCounter][6]=Globals.temperatureWorker.getTemp();
					Globals.dataTable[Globals.dataTableCounter][7]=Globals.pressureWorker.getPress();
					//TODO other entries as well
					Globals.dataTableCounter ++;
				}else{
					this.cancel();
				}
			}		
		}	
	
	}
	
}
